<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/09 13:59
 * Blog：www.myblogs.xyz
 */

namespace app\common\model;


class AdminBalance extends Common
{
    protected $name = 'admin_balance';
    protected $pk = 'balance_id';

    public function balanceInc($where = [], $money = 0, $value = 'balance')
    {
        return $this->setBalance($where, $money, $value, 1);
    }

    public function balanceDec($where = [], $money = 0, $value = 'balance')
    {
        return $this->setBalance($where, $money, $value, 2);
    }

    protected function setBalance($where, $money, $value, $type = 1)
    {
        if (empty($where) || empty($money) || empty($value)) return false;
        $result = $this->where($where)->value($value);
        if ($type == 1){
            $new_result = $result + floatval(round($money, 2));
        }else{
            $new_result = $result - floatval(round($money, 2));
        }
        $rows = $this->where($where)->update([$value=>$new_result]);
        if (empty($rows)){
            return false;
        }
        return true;
    }

}